package net.zhengxinyun.performance.controller.indicators;

import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.dto.SixTownDTO;
import net.zhengxinyun.performance.entity.*;
import net.zhengxinyun.performance.filter.SixTownFilter;
import net.zhengxinyun.performance.filter.SixIndicatorsFilter;
import net.zhengxinyun.performance.service.indicators.SixIndicatorsService;
import net.zhengxinyun.performance.service.indicators.SixScoreService;
import net.zhengxinyun.performance.service.indicators.SixTownService;
import net.zhengxinyun.performance.service.login.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hyp1202 on 2018/11/27.
 */
@RestController
@RequestMapping("/sixIndicators")
public class SixIndicatorsController {
    private SixIndicatorsService sixIndicatorsService;
    private SixTownService sixTownService;
    private TokenService tokenService;
    private SixScoreService sixScoreService;

    @Autowired
    public SixIndicatorsController(SixIndicatorsService sixIndicatorsService, SixTownService sixTownService, TokenService tokenService, SixScoreService sixScoreService) {
        this.sixIndicatorsService = sixIndicatorsService;
        this.sixTownService = sixTownService;
        this.tokenService = tokenService;
        this.sixScoreService = sixScoreService;
    }

    @PostMapping("/insert")
    public Result<String> insert(@RequestBody SixIndicatorsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            if (param.list == null){
                String str = sixIndicatorsService.insert(param.sixIndicatorsEntity,param.townList);
                if (!"添加成功".equals(str)){
                    return ResultUtils.getResults(Result.Status.ERROR,str);
                }else {
                    return ResultUtils.getSuccessResults();
                }
            }else {
                String str = sixIndicatorsService.ListInsert(param.list);
                if (!"添加成功".equals(str)){
                    return ResultUtils.getResults(Result.Status.ERROR,str);
                }else {
                    return ResultUtils.getSuccessResults();
                }
            }

        }
        return ResultUtils.getResults(Result.Status.OVERTIME);
    }

    @PostMapping("/delete")
    public Result<String> delete(@RequestBody SixIndicatorsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = sixIndicatorsService.delete(param.sixIndicatorsEntity);
            if (!"删除成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            else {
                return ResultUtils.getSuccessResults();
            }
        }

        return ResultUtils.getResults(Result.Status.OVERTIME);
    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody SixIndicatorsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = sixIndicatorsService.update(param.sixIndicatorsEntity,param.townList);
            if (!"更新成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME);
    }

    @PostMapping("/query")
    public Result<HashMap<String,Object>> query(@RequestBody SixIndicatorsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            if (param.sixIndicatorsFilter.getPageSize() == 0){
                param.sixIndicatorsFilter.setPageSize(10);
            }
            HashMap<String,Object> map = new HashMap<>();
            SixIndicatorsFilter filter = new SixIndicatorsFilter();
            filter.setIndexType("一级指标");
            List<SixIndicatorsFilter> firstIndexList = sixIndicatorsService.query(filter);
            map.put("firstIndex",firstIndexList);//给一级指标集合
            Double pageTotal = 0.0;
            if ("一级指标".equals(param.sixIndicatorsFilter.getIndexType())){
                List<SixIndicatorsFilter>  list =  sixIndicatorsService.query(param.sixIndicatorsFilter);
                if (list.size()==0){
                    param.sixIndicatorsFilter.setPageNumber(param.sixIndicatorsFilter.getPageNumber()-1);
                    list = sixIndicatorsService.query(param.sixIndicatorsFilter);
                }
                map.put("list",list);
                pageTotal = Util.intGetTotalPage(sixIndicatorsService.queryForCount(param.sixIndicatorsFilter),param.sixIndicatorsFilter.getPageSize());
            }else if ("二级指标".equals(param.sixIndicatorsFilter.getIndexType())){
                List<Map<String,Object>> secList = sixIndicatorsService.querySec(param.sixIndicatorsFilter);
                if (secList.size() == 0){
                    param.sixIndicatorsFilter.setPageNumber(param.sixIndicatorsFilter.getPageNumber()-1);
                    secList = sixIndicatorsService.querySec(param.sixIndicatorsFilter);
                }
                map.put("list",secList);
                pageTotal = Util.intGetTotalPage(sixIndicatorsService.queryForCount(param.sixIndicatorsFilter),param.sixIndicatorsFilter.getPageSize());
            }
            map.put("pageTotal",pageTotal);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }
}

class SixIndicatorsParam{
    public SixIndicatorsFilter sixIndicatorsFilter;
    public SixIndicatorsEntity sixIndicatorsEntity;
    public TokenEntity tokenEntity;
    public List<SixTownEntity> townList;
    public SixTownEntity sixTownEntity;
    public SixTownFilter sixTownFilter;
    public List<SixTownDTO> list;
    public SixTownDTO sixTownDTO;
}
